package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.cs157a.controller.AposApplicationController;
import com.cs157a.dao.PartType;
import com.cs157a.dao.RdiPart;

/**
 * This class displays table detail of a part in the bottom right panel of the
 * main view.
 * 
 * @author hhong
 * 
 */
public class PartDetailPanel extends JPanel {
	private static final int FIELD_WIDTH = 20;

	private static final long serialVersionUID = 20012100301L;

	private JButton saveButton;
	private JButton editButton;

	private JLabel vendorLabel;
	private JTextField vendorField;
	private JLabel partNumberLabel;
	private JTextField partNumberField;
	private JLabel coreLabel;
	private JTextField coreField;

	private JLabel inHeadLabel;
	private JTextField inHeadField;
	private JLabel outHeadLabel;
	private JTextField outHeadField;
	private JLabel inConLabel;
	private JTextField inConField;
	private JLabel outConLabel;
	private JTextField outConField;
	private JLabel tMountLabel;
	private JTextField tMountField;
	private JLabel oilCoolLabel;
	private JTextField oilCoolField;
	private JLabel priceLabel;
	private JTextField priceField;

	private JLabel amountLabel;
	private JTextField amountField;
	private JLabel typeLabel;
	private JComboBox typeCombo;

	private AposApplicationController controller;

	private RdiPart oldPart = new RdiPart();

	public PartDetailPanel(AposApplicationController controller) {
		this.controller = controller;
		controller.setPartDetailPanel(this);

		setLayout(new BorderLayout());

		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
		panel.add(createInnerPanel());
		JScrollPane scrollPane = new JScrollPane(panel);

		add(scrollPane, BorderLayout.CENTER);
		add(createButtonPanel(controller), BorderLayout.SOUTH);
	}

	public void display(RdiPart part) {
		resetGui();
		vendorField.setText(part.getVendor());
		partNumberField.setText(part.getPartNumber());
		coreField.setText(part.getCore());
		inHeadField.setText(part.getInHead());
		outHeadField.setText(part.getOutHead());
		inConField.setText(part.getInCon());
		outConField.setText(part.getOuCon());
		tMountField.setText(part.getTMount());
		oilCoolField.setText(part.getOilCool());
		priceField.setText("" + part.getPrice());
		amountField.setText("" + part.getAmount());
		typeCombo.setSelectedItem(part.getType());
		if (part.getType() == null) {
			typeCombo.setEnabled(false);
		}
	}

	private JPanel createButtonPanel(final AposApplicationController controller) {
		initSaveButton(controller);
		initEditButton(controller);

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(saveButton);
		buttonPanel.add(editButton);
		return buttonPanel;
	}

	private void savePerformed() {
		RdiPart part = new RdiPart();
		part.setPartNumber(partNumberField.getText());
		part.setCore(coreField.getText());
		part.setAmount(Integer.parseInt(amountField.getText()));
		part.setPrice(Float.parseFloat(priceField.getText()));
		part.setInCon(inConField.getText());
		part.setInHead(inHeadField.getText());
		part.setOilCool(oilCoolField.getText());
		part.setOuCon(outConField.getText());
		part.setOutHead(outHeadField.getText());
		part.setTMount(tMountField.getText());
		part.setVendor(vendorField.getText());
		part.setType((PartType) typeCombo.getSelectedItem());

		controller.updatePart(oldPart, part);
		saveButton.setEnabled(false);
		setEditableAllFields(false);
	}

	private void initSaveButton(final AposApplicationController controller) {
		saveButton = new JButton(CommonString.SAVE);
		saveButton.setEnabled(false);
		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				savePerformed();
			}
		});
	}

	private void setOldPart() {
		// RdiPart oldPart = new RdiPart();
		oldPart.setPartNumber(partNumberField.getText());
		oldPart.setCore(coreField.getText());
		oldPart.setAmount(Integer.parseInt(amountField.getText()));
		oldPart.setPrice(Float.parseFloat(priceField.getText()));
		oldPart.setInCon(inConField.getText());
		oldPart.setInHead(inHeadField.getText());
		oldPart.setOilCool(oilCoolField.getText());
		oldPart.setOuCon(outConField.getText());
		oldPart.setOutHead(outHeadField.getText());
		oldPart.setTMount(tMountField.getText());
		oldPart.setVendor(vendorField.getText());
		oldPart.setType((PartType) typeCombo.getSelectedItem());

	}

	private void initEditButton(final AposApplicationController controller) {
		editButton = new JButton(CommonString.EDIT);
		editButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setOldPart();
				setEditableAllFields(true);
				saveButton.setEnabled(true);
			}
		});
	}

	private void setEditableAllFields(boolean editable) {
		vendorField.setEditable(false);
		partNumberField.setEditable(editable);
		coreField.setEditable(editable);
		inHeadField.setEditable(editable);
		outHeadField.setEditable(editable);
		inConField.setEditable(editable);
		outConField.setEditable(editable);
		tMountField.setEditable(editable);
		oilCoolField.setEditable(editable);
		priceField.setEditable(editable);
		amountField.setEditable(editable);
		typeCombo.setEditable(editable);
	}

	private JPanel createInnerPanel() {
		vendorLabel = new JLabel(PartColumn.VENDOR.getName());
		vendorField = new JTextField(FIELD_WIDTH);

		partNumberLabel = new JLabel(PartColumn.PART_NUMBER.getName());
		partNumberField = new JTextField(FIELD_WIDTH);

		coreLabel = new JLabel(PartColumn.CORE.getName());
		coreField = new JTextField(FIELD_WIDTH);

		inHeadLabel = new JLabel(PartColumn.IN_HEAD.getName());
		outHeadLabel = new JLabel(PartColumn.OUT_HEAD.getName());
		inConLabel = new JLabel(PartColumn.IN_CON.getName());
		outConLabel = new JLabel(PartColumn.OUT_CON.getName());
		tMountLabel = new JLabel(PartColumn.T_MOUNT.getName());
		oilCoolLabel = new JLabel(PartColumn.OIL_COOL.getName());
		priceLabel = new JLabel(PartColumn.PRICE.getName());
		amountLabel = new JLabel(PartColumn.AMOUNT.getName());
		typeLabel = new JLabel(PartColumn.TYPE.getName());

		inHeadField = new JTextField(FIELD_WIDTH);
		outHeadField = new JTextField(FIELD_WIDTH);
		inConField = new JTextField(FIELD_WIDTH);
		outConField = new JTextField(FIELD_WIDTH);
		tMountField = new JTextField(FIELD_WIDTH);
		coreField = new JTextField(FIELD_WIDTH);
		oilCoolField = new JTextField(FIELD_WIDTH);
		priceField = new JTextField(FIELD_WIDTH);
		amountField = new JTextField(FIELD_WIDTH);
		typeCombo = new JComboBox(PartType.values());

		setEditableAllFields(false);

		JPanel innerPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.fill = GridBagConstraints.HORIZONTAL;

		innerPanel.add(vendorLabel, c);

		c.gridx = 0;
		c.gridy = 1;
		innerPanel.add(partNumberLabel, c);

		c.gridx = 0;
		c.gridy = 2;
		innerPanel.add(coreLabel, c);

		c.gridx = 0;
		c.gridy = 3;
		innerPanel.add(inHeadLabel, c);

		c.gridx = 0;
		c.gridy = 4;
		innerPanel.add(outHeadLabel, c);

		c.gridx = 0;
		c.gridy = 5;
		innerPanel.add(inConLabel, c);
		c.gridx = 0;
		c.gridy = 6;
		innerPanel.add(outConLabel, c);

		c.gridx = 0;
		c.gridy = 7;
		innerPanel.add(tMountLabel, c);
		c.gridx = 0;
		c.gridy = 8;
		innerPanel.add(oilCoolLabel, c);
		c.gridx = 0;
		c.gridy = 9;
		innerPanel.add(priceLabel, c);
		c.gridx = 0;
		c.gridy = 10;
		innerPanel.add(amountLabel, c);
		c.gridx = 0;
		c.gridy = 11;
		innerPanel.add(typeLabel, c);

		c.gridx = 1;
		c.gridy = 0;
		innerPanel.add(vendorField, c);

		c.gridx = 1;
		c.gridy = 1;
		innerPanel.add(partNumberField, c);

		c.gridx = 1;
		c.gridy = 2;
		innerPanel.add(coreField, c);

		c.gridx = 1;
		c.gridy = 3;
		innerPanel.add(inHeadField, c);

		c.gridx = 1;
		c.gridy = 4;
		innerPanel.add(outHeadField, c);

		c.gridx = 1;
		c.gridy = 5;
		innerPanel.add(inConField, c);

		c.gridx = 1;
		c.gridy = 6;
		innerPanel.add(outConField, c);

		c.gridx = 1;
		c.gridy = 7;
		innerPanel.add(tMountField, c);

		c.gridx = 1;
		c.gridy = 8;
		innerPanel.add(oilCoolField, c);

		c.gridx = 1;
		c.gridy = 9;
		innerPanel.add(priceField, c);

		c.gridx = 1;
		c.gridy = 10;
		innerPanel.add(amountField, c);

		c.gridx = 1;
		c.gridy = 11;
		innerPanel.add(typeCombo, c);

		return innerPanel;
	}

	/**
	 * Create the GUI and show it. For thread safety, this method should be
	 * invoked from the event-dispatching thread.
	 */
	private static void createAndShowGUI() {
		// Create and set up the window.
		JFrame frame = new JFrame("Test");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.getContentPane().add(new PartDetailPanel(null),
				BorderLayout.CENTER);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	protected void clearAllFields() {
		vendorField.setText(" ");
		partNumberField.setText(" ");
		coreField.setText(" ");
		inHeadField.setText(" ");
		outHeadField.setText(" ");
		inConField.setText(" ");
		outConField.setText(" ");
		tMountField.setText(" ");
		oilCoolField.setText(" ");
		priceField.setText(" ");
		amountField.setText(" ");
	}

	public void reset() {
		clearAllFields();
		resetGui();
	}

	private void resetGui() {
		setEditableAllFields(false);
		saveButton.setEnabled(false);
		editButton.setEnabled(true);
	}
}
